package leeon.bbsbrower.views;

import ipworks.IPWorksException;
import leeon.bbsbrower.actions.BBSMsgAction;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

public class SendMsgView extends ViewPart {
	public static final String ID = "bbsbrower.view.sendmsg";
	
	private Text id = null;
	private Text msg = null;
	private Text all = null;


	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		//布局
		parent.setLayout(null);
				
		id = new Text(parent, SWT.BORDER);		
		id.setBounds(90, 10, 200, 30);		
		id.setFont(ApplicationWorkbenchWindowAdvisor.listFont);		
		id.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {
				//回车到msg
				if (e.keyCode == 13) {
					msg.setFocus();
					e.doit = false;	
				//esc 退出
				} else if (e.keyCode == SWT.ESC) {
					actionToFriend();					
					e.doit = false;	
				//Ctrl + z查看以前的短信	
				} else if (e.stateMask == SWT.CTRL && e.keyCode == 122) {
					all.setVisible(!all.getVisible());
					actionAllMsg();
					e.doit = false;
				}
			}

			public void keyReleased(KeyEvent e) {}
			
		});		
		final Label idLabel = new Label(parent, SWT.NONE);
		idLabel.setText("对方ID:");
		idLabel.setBounds(10, 10, 70, 30);
		idLabel.setFont(ApplicationWorkbenchWindowAdvisor.listFont);
		
		msg = new Text(parent, SWT.BORDER);
		msg.setBounds(90, 50, 900, 30);
		msg.setFont(ApplicationWorkbenchWindowAdvisor.listFont);
		msg.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {
				//回车发送
				if (e.keyCode == 13) {
					actionSend();
					e.doit = false;
				//退出
				} else if (e.keyCode == SWT.ESC) {
					actionToFriend();					
					e.doit = false;	
				//Ctrl + z查看以前的短信	
				} else if (e.stateMask == SWT.CTRL && e.keyCode == 122) {
					all.setVisible(!all.getVisible());
					actionAllMsg();
					e.doit = false;
				}
			}

			public void keyReleased(KeyEvent e) {}
			
		});
		final Label msgLabel = new Label(parent, SWT.NONE);
		msgLabel.setText("消息:");
		msgLabel.setBounds(10, 50, 70, 30);
		msgLabel.setFont(ApplicationWorkbenchWindowAdvisor.listFont);
		

		all = new Text(parent, SWT.BORDER|SWT.READ_ONLY|SWT.V_SCROLL);		
		all.setBounds(90, 90, 900, 550);	
		all.setFont(ApplicationWorkbenchWindowAdvisor.listFont);
		all.setVisible(false);
		all.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {
				//回车发送
				if (e.keyCode == 13) {
					msg.setFocus();
					e.doit = false;
				//esc
				} else if (e.keyCode == SWT.ESC) {
					actionToFriend();					
					e.doit = false;	
				//Ctrl + z查看以前的短信	
				} else if (e.stateMask == SWT.CTRL && e.keyCode == 122) {
					all.setVisible(!all.getVisible());
					msg.setFocus();
					actionAllMsg();
					e.doit = false;
				}
			}

			public void keyReleased(KeyEvent e) {}
			
		});
	}
	
	
	/**
	 * 发送的action
	 *
	 */
	private void actionSend() {
		if (id.getText() == null || id.getText().length() == 0) {
			MessageDialog.openWarning(this.getViewSite().getShell(), "消息发送", "对方ID不能为空");
			this.id.setFocus();
			return;
		}
		
		if (msg.getText() == null || msg.getText().length() == 0) {
			MessageDialog.openWarning(this.getViewSite().getShell(), "消息发送", "发送的消息不能为空");
			this.msg.setFocus();
			return;
		}
		
		try {
			BBSMsgAction.sendMsg(id.getText(), msg.getText(), null);
			id.setText("");
			msg.setText("");
			id.setFocus();
			
		} catch (Exception e) {
			MessageDialog.openError(this.getViewSite().getShell(), "错误信息", e.toString());
			id.setFocus();
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 回好友界面
	 *
	 */
	private void actionToFriend() {
		//跳转回到用户操作列表
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			FriendView frd = (FriendView)page.showView(FriendView.ID);
			frd.refreshData(3);
		} catch (PartInitException e) {
			e.printStackTrace();
		}

	}
	
	/**
	 * 查看以前的短信
	 *
	 */
	private void actionAllMsg() {
		try {
			all.setText(BBSMsgAction.getAllMsg());
		} catch (IPWorksException e) {
			MessageDialog.openError(this.getViewSite().getShell(), "错误信息", e.toString());
			e.printStackTrace();
		}
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		id.setFocus();
	}
}